#include "c3d.h"
#include "c3dTransform.h"

void c3dTransform::LookAt(mat4x4& m, vec4& eye, vec4& at, vec4& up)
{
	vec3 xaxis, yaxis;
	vec3 zaxis;
	vec4 z = at - eye;
	zaxis = vec3(z);
	normalize(zaxis);
	xaxis = cross(vec3(up), zaxis);
	normalize(xaxis);
	yaxis = cross(zaxis, xaxis);

	m[0][0] = xaxis.x;
	m[1][0] = xaxis.y;
	m[2][0] = xaxis.z;
	m[3][0] = -1 * dot(xaxis, vec3(eye));
	// m[3][0] = -vector_dotproduct(&xaxis, eye);
	m[0][1] = yaxis.x;
	m[1][1] = yaxis.y;
	m[2][1] = yaxis.z;
	m[3][1] = -1 * dot(yaxis, vec3(eye));
	//	 m[3][1] = -vector_dotproduct(&yaxis, eye);
	m[0][2] = zaxis.x;
	m[1][2] = zaxis.y;
	m[2][2] = zaxis.z;
	m[3][2] = dot(zaxis, vec3(eye));
	//	 m[3][2] = -vector_dotproduct(&zaxis, eye);
	m[0][3] = m[1][3] = m[2][3] = 0.0f;
	m[3][3] = 1.0f;
}

void c3dTransform::LookAt(c3dCamera cam)
{
	LookAt(mview, cam.pos, cam.dir, cam.upDir);
}

void c3dTransform::perspective(mat4x4& m, float fovy, float aspect, float zn, float zf)
{
	float fax = 1.0f / (float)tan(fovy * 0.5f);

	SetZero(m);
	m[0][0] = (float)(fax / aspect);
	m[1][1] = (float)(fax);
	m[2][2] = zf / (zf - zn);
	m[3][2] = -zn * zf / (zf - zn);
	m[2][3] = 1;
}

void c3dTransform::update()
{
	transform = mworld * mview * project;
	//transform = mworld * project;
	//transform = mworld; //* project;
}
